package com.yzh.wanandroid.vm

import android.app.Application
import android.util.Log
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.paging.Pager
import androidx.paging.PagingConfig
import com.yzh.wanandroid.base.BaseFragmentViewModel
import com.yzh.wanandroid.model.StateData
import com.yzh.wanandroid.network.retrofit.responsebean.BaseData
import com.yzh.wanandroid.network.retrofit.responsebean.UserInfo
import com.yzh.wanandroid.repository.datasource.IntegralListRepository

class UserInfoFragmentVm(application: Application) : BaseFragmentViewModel(application) {

    private val TAG = "UserInfoFragmentVm"

    var mCoinCount = MutableLiveData<Int>()

    override fun onCreate(owner: LifecycleOwner) {
        Log.i(TAG, "onCreate ok")
        val userInfoData = MutableLiveData<StateData<BaseData<UserInfo>>>()
        mUserRepository.loadUserInfo(owner, userInfoData)
        userInfoData.observe(owner) {
            mCoinCount.value = it.data?.data?.coinCount
        }
    }

    /**
     * 通过paging3加载积分列表
     */
    fun getData() = Pager(PagingConfig(pageSize = 2)) {
        IntegralListRepository()
    }.flow
}